﻿using System;
using System.Collections.Generic;
using Terraria.Achievements;

namespace Terraria.GameContent.Achievements
{
	// Token: 0x02000469 RID: 1129
	public class ItemCraftCondition : AchievementCondition
	{
		// Token: 0x06002661 RID: 9825 RVA: 0x0001AAD7 File Offset: 0x00018CD7
		private ItemCraftCondition(short itemId) : base("ITEM_PICKUP_" + itemId)
		{
			this._itemIds = new short[]
			{
				itemId
			};
			ItemCraftCondition.ListenForCraft(this);
		}

		// Token: 0x06002662 RID: 9826 RVA: 0x0001AB05 File Offset: 0x00018D05
		private ItemCraftCondition(short[] itemIds) : base("ITEM_PICKUP_" + itemIds[0])
		{
			this._itemIds = itemIds;
			ItemCraftCondition.ListenForCraft(this);
		}

		// Token: 0x06002663 RID: 9827 RVA: 0x00491960 File Offset: 0x0048FB60
		private static void ListenForCraft(ItemCraftCondition condition)
		{
			if (!ItemCraftCondition._isListenerHooked)
			{
				AchievementsHelper.OnItemCraft += ItemCraftCondition.ItemCraftListener;
				ItemCraftCondition._isListenerHooked = true;
			}
			for (int i = 0; i < condition._itemIds.Length; i++)
			{
				if (!ItemCraftCondition._listeners.ContainsKey(condition._itemIds[i]))
				{
					ItemCraftCondition._listeners[condition._itemIds[i]] = new List<ItemCraftCondition>();
				}
				ItemCraftCondition._listeners[condition._itemIds[i]].Add(condition);
			}
		}

		// Token: 0x06002664 RID: 9828 RVA: 0x004919E4 File Offset: 0x0048FBE4
		private static void ItemCraftListener(short itemId, int count)
		{
			if (ItemCraftCondition._listeners.ContainsKey(itemId))
			{
				List<ItemCraftCondition> list = ItemCraftCondition._listeners[itemId];
				foreach (ItemCraftCondition itemCraftCondition in list)
				{
					itemCraftCondition.Complete();
				}
			}
		}

		// Token: 0x06002665 RID: 9829 RVA: 0x0001AB2C File Offset: 0x00018D2C
		public static AchievementCondition Create(params short[] items)
		{
			return new ItemCraftCondition(items);
		}

		// Token: 0x06002666 RID: 9830 RVA: 0x0001AB34 File Offset: 0x00018D34
		public static AchievementCondition Create(short item)
		{
			return new ItemCraftCondition(item);
		}

		// Token: 0x06002667 RID: 9831 RVA: 0x00491A4C File Offset: 0x0048FC4C
		public static AchievementCondition[] CreateMany(params short[] items)
		{
			AchievementCondition[] array = new AchievementCondition[items.Length];
			for (int i = 0; i < items.Length; i++)
			{
				array[i] = new ItemCraftCondition(items[i]);
			}
			return array;
		}

		// Token: 0x0400419D RID: 16797
		private const string Identifier = "ITEM_PICKUP";

		// Token: 0x0400419E RID: 16798
		private static Dictionary<short, List<ItemCraftCondition>> _listeners = new Dictionary<short, List<ItemCraftCondition>>();

		// Token: 0x0400419F RID: 16799
		private static bool _isListenerHooked = false;

		// Token: 0x040041A0 RID: 16800
		private short[] _itemIds;
	}
}
